#include "CMainState.h"



CMainState::CMainState(CStateManager *state) : IGameState(state)
{
	
}

CMainState::~CMainState()
{

}


void CMainState::Init()
{
	InitMenuItem();
	m_iStateGame = ::ID_STATE_OPTION;
	m_iSpeedLevel = 1;
	m_iChessWidth = m_iChessHeight = 8;
	m_iKnightPosX = m_iKnightPosY = 0;


	D3DXCreateSprite(m_StateManager->m_Game->GetDevice(), &m_pSprite);
	
	
	

	////////////////////////////////////////////////////////////////////////////////
	m_pNumber = new CNumber(m_StateManager->m_Game->GetDevice(), Name_Number);

	m_pSkybox = new CSkybox();
	m_pSkybox->InitSkyboxTexture(m_StateManager->m_Game->GetDevice());
	m_pSkybox->BuildSkybox(m_StateManager->m_Game->GetDevice());


	m_pCamera = new CCamera();
	m_pCamera->SetPosition(D3DXVECTOR3(50, 150, -220));
	m_pCamera->SetLook(D3DXVECTOR3(80, 0, -110));	
	D3DXMatrixLookAtLH(&m_mtxView, &D3DXVECTOR3(50.0f, 150.0f, -220.0f), &D3DXVECTOR3(80.0f, 0.0f, -110.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
	m_StateManager->m_Game->GetDevice()->SetTransform(D3DTS_VIEW, &m_mtxView);

	m_pChessBoard = new ChessBoard(m_iChessWidth, m_iChessHeight, m_StateManager->m_Game->GetDevice());
	m_pChessBoard->InitChessBoard();
	m_pKnight = new Knight(m_StateManager->m_Game->GetDevice());
	
}


void CMainState::Update()
{
	switch (m_iStateGame)
	{
	case ID_STATE_OPTION:
		UpdateScreen2D();
		break;
	case ID_STATE_RUNTIME:
		UpdateScreen3D();
		break;
	}
}


void CMainState::UpdateScreen2D()
{
	if (::MOUSE_GAME_LCLICKDOWN)
	{
		//
		if (m_miButton[::MID_START]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			m_iStateGame = ::ID_STATE_RUNTIME;
			CalcMovement();
		}


		// 
		if (m_miButton[::MID_EXIT]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			PostQuitMessage(0);
		}


		//
		if (m_miButton[::MIU_KNIGHT_SPEED]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iSpeedLevel < ::SPEED_MAX)
			{
				m_iSpeedLevel++;
				m_pKnight->SetSpeedKnight(m_iSpeedLevel * 1.5f, m_iSpeedLevel * 0.3f + 0.3f);
			}
		}


		//
		if (m_miButton[::MID_KNIGHT_SPEED]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iSpeedLevel > 1)
			{
				m_iSpeedLevel--;
				m_pKnight->SetSpeedKnight(m_iSpeedLevel * 1.5f, m_iSpeedLevel * 0.3f + 0.3f);
			}
		}


		//
		if (m_miButton[::MIU_KNIGHT_POSX]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iKnightPosX < m_iChessWidth - 1)
				m_iKnightPosX++;
		}


		//
		if (m_miButton[::MID_KNIGHT_POSX]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iKnightPosX > 0)
				m_iKnightPosX--;
		}


		//
		if (m_miButton[::MIU_KNIGHT_POSY]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iKnightPosY < m_iChessWidth - 1)
				m_iKnightPosY++;
		}


		//
		if (m_miButton[::MID_KNIGHT_POSY]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iKnightPosY > 0)
				m_iKnightPosY--;
		}


		//
		if (m_miButton[::MIU_CHESS_WIDTH]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iChessWidth < ::MAX_CHESS_WIDTH)
				m_iChessWidth++;
		}


		//
		if (m_miButton[::MID_CHESS_WIDTH]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iChessWidth > ::MIN_CHESS_WIDTH)
				m_iChessWidth--;
			if (m_iKnightPosX == m_iChessWidth)
				m_iKnightPosX--;
		}


		//
		if (m_miButton[::MIU_CHESS_HEIGHT]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iChessHeight < ::MAX_CHESS_HEIGHT)
				m_iChessHeight++;
		}


		//
		if (m_miButton[::MID_CHESS_HEIGHT]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			if (m_iChessHeight > ::MIN_CHESS_HEIGHT)
				m_iChessHeight--;
			if (m_iKnightPosY == m_iChessHeight)
				m_iKnightPosY--;
		}


		// Reset all mouse state & mouse location
		::ResetMouseActive();
	}
}


void CMainState::UpdateScreen3D()
{

	if (::MOUSE_GAME_LCLICKDOWN)
	{

		if (m_miButton[::MID_BACK]->IsClick(::MOUSE_GAME_X, ::MOUSE_GAME_Y))
		{
			m_iStateGame = ::ID_STATE_OPTION;
		}



		// Reset all mouse state & mouse location
		::ResetMouseActive();
		::ResetMouseLocation();
	}


	m_pKnight->PlayScript(m_pChessBoard->m_ppBoard);
	D3DXVECTOR3 position;
	m_pCamera->GetPosition(&position);
	D3DXMatrixTranslation(&m_pSkybox->m_mtxWorld, 50, 150, -220.0f);

	m_pCamera->GetMatrixView(&m_mtxView);
	m_StateManager->m_Game->GetDevice()->SetTransform(D3DTS_VIEW, &m_mtxView);
}


void CMainState::Render()
{
	m_StateManager->m_Game->GetDevice()->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffff, 1.0f, 0);
	m_StateManager->m_Game->GetDevice()->BeginScene();
	
	switch (m_iStateGame)
	{
	case ID_STATE_OPTION:
		DrawScreen2D();
		break;
	case ID_STATE_RUNTIME:
		DrawScreen3D();
		break;
	}

	m_StateManager->m_Game->GetDevice()->EndScene();
	m_StateManager->m_Game->GetDevice()->Present(0, 0, 0, 0);

}


void CMainState::DrawScreen2D()
{
	m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);

	DrawMenuItem();
	DrawNumber();

	m_pSprite->End();
}


void CMainState::DrawNumber()
{
	m_pNumber->DrawCenter(m_pSprite, 490, 156, m_iSpeedLevel);
	m_pNumber->DrawCenter(m_pSprite, 490, 227, m_iKnightPosX + 1);
	m_pNumber->DrawCenter(m_pSprite, 490, 298, m_iKnightPosY + 1);
	m_pNumber->DrawCenter(m_pSprite, 490, 369, m_iChessWidth);
	m_pNumber->DrawCenter(m_pSprite, 490, 440, m_iChessHeight);

}


void CMainState::DrawScreen3D()
{
	

	m_pSkybox->SetRenderState(m_StateManager->m_Game->GetDevice());
	m_pSkybox->RenderSkybox(m_StateManager->m_Game->GetDevice());

	m_pChessBoard->SetRenderState();
	m_pChessBoard->DrawChessBoard();

	m_pKnight->RenderKnight();


	m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
	m_miButton[::MID_BACK]->DrawSprite(m_pSprite);
	m_pSprite->End();
}


void CMainState::Destroy()
{

}


void CMainState::InitMenuItem()
{
	m_miButton	= new CMenuItem* [12];

	for (int i = 0; i < 5; i++)
	{
		m_miButton[i] = new CMenuItem();
		m_miButton[i]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Bt_Name_Up);
		m_miButton[i]->SetCenterPosition(209, 156 + 71 * i);

		m_miButton[i + 5] = new CMenuItem();
		m_miButton[i + 5]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Bt_Name_Down);
		m_miButton[i + 5]->SetCenterPosition(589, 156 + 71 * i);
	}

	m_miButton[::MID_START] = new CMenuItem();
	m_miButton[::MID_START]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Bt_Name_Start);
	m_miButton[::MID_START]->SetCenterPosition(282, 527);

	m_miButton[::MID_EXIT] = new CMenuItem();
	m_miButton[::MID_EXIT]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Bt_Name_Exit);
	m_miButton[::MID_EXIT]->SetCenterPosition(503, 527);

	m_miButton[::MID_BACK] = new CMenuItem();
	m_miButton[::MID_BACK]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Bt_Name_Back);
	m_miButton[::MID_BACK]->SetCenterPosition(700, 35);



	m_miLabel = new CMenuItem* [6];
	for (int i = 0; i < 5; i++)
	{
		m_miLabel[i] = new CMenuItem();
	}

	m_miLabel[::LID_KNIGHT_SPEED]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Lb_Name_KSpeed);
	m_miLabel[::LID_KNIGHT_POSX]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Lb_Name_KPosX);
	m_miLabel[::LID_KNIGHT_POSY]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Lb_Name_KPosY);
	m_miLabel[::LID_CHESS_WIDTH]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Lb_Name_CWidth);
	m_miLabel[::LID_CHESS_HEIGHT]->LoadSprite(m_StateManager->m_Game->GetDevice(), ::Lb_Name_CHeight);

	for (int i = 0; i < 5; i++)
	{
		m_miLabel[i]->SetCenterPosition(398, 156 + 71 * i);
	}


	m_miLabel[5] = new CMenuItem();
	m_miLabel[5]->LoadSprite(m_StateManager->m_Game->GetDevice(), Name_Backgroud);
	m_miLabel[5]->SetPosition(-5, 0);
}


void CMainState::DrawMenuItem()
{
	m_miLabel[5]->DrawSprite(m_pSprite);

	for (int i = 0; i < 12; i++)
	{
		m_miButton[i]->DrawSprite(m_pSprite);
	}

	for (int i = 0; i < 5; i++)
	{
		m_miLabel[i]->DrawSprite(m_pSprite);
	}
}


void CMainState::CalcMovement()
{
	m_pChessBoard->SetSize(m_iChessWidth, m_iChessHeight);
	m_pChessBoard->SetKnightTour(m_iKnightPosX, m_iKnightPosY, 0, 0, ChessBoard::TYPE_FREE);
	m_pChessBoard->CalcMoveMent();

	m_pKnight->ResetAll();
	m_pKnight->SetArrMovement(m_pChessBoard->m_pArrStep, m_pChessBoard->GetWidth(), m_pChessBoard->GetHeight());
}